<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/shop_car.css">
    <script src="js/vue.js"></script>

</head>

<body>
    <div id="app">
        <div class="all" v-if="list.length===0">当前购物车为空</div>
        <!-- template空标签，不占据页面位置 -->
        <template v-else>
               <div class="all"><input type="checkbox" v-model="isChecked" @change="allCheck()">
            全选|全不选
            </div>
            <ul>
                <li v-for="(item,index) in list" :key="item.id">
                    <div>
                        <input type="checkbox" name="" id="" v-model="checkGroup" :value="item" @change="isAllCheck()" />
        
                    </div>
                    <div>
                        <img :src="item.pic">
                    </div>
                    <div>
                        <div>名称:{{item.name}}</div>
                        <div>价格:￥{{item.price}}</div>
                    </div>
                    <div>
                        <button @click="item.number--" :disabled="item.number===1">-</button>
                        <span>{{item.number}}</span>
                        <button @click="item.number++" :disabled="item.number===item.limit">+</button>
                    </div>
                    <div>
                        <button @click="deleteGood(index,item.id)">删除</button>
                    </div>
                </li>
            </ul>
            <div class="all">总金额：{{totalPrice()}}</div>
           </template>
    </div>


    <script>
        var vm = new Vue({
            el: "#app",
            data: {
                isChecked: false,
                checkGroup: [],
                list: [{
                    name: "商品1",
                    price: 12,
                    number: 2,
                    id: 1,
                    // 限购
                    limit: 3,
                    pic: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13935227792%2F641&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633076290&t=5f4eacc9a34bc883a698183992d53db8"
                }, {
                    name: "商品2",
                    price: 22,
                    number: 6,
                    id: 2,
                    // 限购
                    limit: 8,
                    pic: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13935227792%2F641&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633076290&t=5f4eacc9a34bc883a698183992d53db8"
                }, {
                    name: "商品3",
                    price: 42,
                    number: 4,
                    id: 3,
                    // 限购
                    limit: 10,
                    pic: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13935227792%2F641&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633076290&t=5f4eacc9a34bc883a698183992d53db8"
                }, {
                    name: "商品4",
                    price: 90,
                    number: 3,
                    id: 4,
                    // 限购
                    limit: 5,
                    pic: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13935227792%2F641&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633076290&t=5f4eacc9a34bc883a698183992d53db8"
                }, {
                    name: "商品5",
                    price: 19,
                    number: 2,
                    id: 5,
                    // 限购
                    limit: 5,
                    pic: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13935227792%2F641&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633076290&t=5f4eacc9a34bc883a698183992d53db8"
                }]
            },
            methods: {
                totalPrice() {
                    var total = 0;
                    this.checkGroup.forEach((element) => {
                        total += element.price * element.number;

                    });
                    return total
                },

                deleteGood(index, goodId) {
                    this.list.splice(index, 1);
                    this.checkGroup = this.checkGroup.filter(element => element.id !== goodId)
                    this.isAllCheck();
                },
                allCheck() {
                    if (this.isChecked) {
                        this.checkGroup = this.list
                    } else {
                        this.checkGroup = [];
                    }
                },
                // 选中单个商品时，判断是否为全部选中
                isAllCheck() {
                    if (this.checkGroup.length === this.list.length) {
                        this.isChecked = true;
                    } else {
                        this.isChecked = false;
                    }
                }
            }
        })
    </script>

</body>


</html>